﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Lists;
using System.Data.SqlClient;
using AccountingLibs;

namespace ReportGenerators.QuanLyKho
{
    public partial class FrmThamSoTonMatHang : BasicForms.FrmThamSoBaoCao
    {
        public FrmThamSoTonMatHang()
        {
            InitializeComponent();
        }
        protected override void PrepareDataForReport()
        {
            VatTuHangHoa vatTuHangHoa = DanhMucVatTuHangHoa.Instance.GetByID((Guid)cmbVatTuHangHoa.EditValue);

            SqlConnection con = new SqlConnection(AccountingLibs.LocalSettings.ConnectString);
            DataSet ds = new DataSet();
            try
            {
                SqlCommand cmd = con.CreateCommand();
                con.Open();
                SqlDataAdapter da = null;

                cmd.CommandText = "Select_ThongTinDoanhNghiep";
                cmd.Parameters.Clear();
                da = new SqlDataAdapter(cmd);
                da.Fill(ds, "ThongTinDoanhNghiep");

                DataTable dtChiTietTheKho = new DataTable("ChiTietTonMatHang");
                dtChiTietTheKho.Columns.Add("KyBaoCao", Type.GetType("System.String"));
                dtChiTietTheKho.Columns.Add("MaVTHH", Type.GetType("System.String"));
                dtChiTietTheKho.Columns.Add("TenVTHH", Type.GetType("System.String"));
                dtChiTietTheKho.Columns.Add("DonViTinh", Type.GetType("System.String"));
                dtChiTietTheKho.Columns.Add("Ten", Type.GetType("System.String"));
                dtChiTietTheKho.Rows.Add(dtChiTietTheKho.NewRow());
                dtChiTietTheKho.Rows[0]["KyBaoCao"] = _KyBaoCao.Ten;
                dtChiTietTheKho.Rows[0]["MaVTHH"] = vatTuHangHoa.Ma;
                dtChiTietTheKho.Rows[0]["TenVTHH"] = vatTuHangHoa.Ten;
                dtChiTietTheKho.Rows[0]["DonViTinh"] = vatTuHangHoa.DonViTinh;
                //dtChiTietTheKho.Rows[0]["Ten"] = kho.Ten;
                ds.Tables.Add(dtChiTietTheKho);

                cmd.CommandText = "Select_TonMatHang";
                cmd.CommandType = CommandType.StoredProcedure;
                //cmd.Parameters.AddWithValue("@KhoID", kho.ID);
                cmd.Parameters.AddWithValue("@VatTuHangHoaID", vatTuHangHoa.ID);
                cmd.Parameters.AddWithValue("@NgayBatDau", _KyBaoCao.ThoiDiemDau);
                cmd.Parameters.AddWithValue("@NgayKetThuc", _KyBaoCao.ThoiDiemCuoi);
                da.Fill(ds, "KhoanMucTonMatHang");

                //foreach (Kho kho in DanhMucKho.Instance)
                //{
                    //TonKho tonDauKy = TonKho.CalculateFromDB(cmd, _KyBaoCao.ThoiDiemDau, kho.ID, vatTuHangHoa.ID);
                    //DataRow dauKyRow = ds.Tables["KhoanMucTonMatHang"].NewRow();
                    //dauKyRow["NhapKho"] = 1;
                    //dauKyRow["NgayChungTu"] = _KyBaoCao.ThoiDiemDau;
                    //dauKyRow["SoHieuPhieuNhap"] = "";
                    //dauKyRow["SoHieuPhieuNhap"] = "";
                    //dauKyRow["DienGiai"] = "Tồn đầu kỳ";
                    //dauKyRow["Ngay"] = _KyBaoCao.ThoiDiemDau;
                    //dauKyRow["SoLuongNhap"] = tonDauKy.SoLuong;
                    //dauKyRow["SoLuongXuat"] = 0;
                    //ds.Tables["KhoanMucTonMatHang"].Rows.InsertAt(dauKyRow, 0);

                    ////modify dataset
                    //decimal tonKho = 0;
                    //foreach (DataRow row in ds.Tables["KhoanMucTonMatHang"].Rows)
                    //{
                    //    if ((bool)row["NhapKho"] == true)
                    //    {
                    //        row["SoHieuPhieuXuat"] = "";
                    //        row["SoLuongXuat"] = 0;
                    //        tonKho += (decimal)row["SoLuongNhap"];
                    //    }
                    //    else
                    //    {
                    //        row["SoHieuPhieuNhap"] = "";
                    //        row["SoLuongNhap"] = 0;
                    //        tonKho -= (decimal)row["SoLuongXuat"];
                    //    }
                    //    row["SoLuongTon"] = tonKho;
                    //}
                //}

                _Report = new ReportTemplates.QuanLyKho.TonMatHang.RptTonMatHang();
                _Report.SetDataSource(ds);

            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.ToString());
            }
            finally
            {
                con.Close();
            }
        }

        protected override void PrintReport()
        {
            if (_Report != null)
            {
                ReportTemplates.FrmPrintReport frm = new ReportTemplates.FrmPrintReport(_Report, "Tồn Mặt Hàng");
                frm.Show();
            }
        }

        private void FrmThamSoTonMatHang_Load(object sender, EventArgs e)
        {
            cmbKyBaoCao.SelectedIndex = 0;
            #region cmbVatTuHangHoa
            DataView dvVatTuHangHoa = DanhMucVatTuHangHoa.GetAll().DataView;
            cmbVatTuHangHoa.DataSource = dvVatTuHangHoa;
            cmbVatTuHangHoa.DisplayColumn = "Ten";
            cmbVatTuHangHoa.ValueColumn = "ID";
            cmbVatTuHangHoa.AddVisibleColumn("Ma", "Mã");
            cmbVatTuHangHoa.AddVisibleColumn("Ten", "Tên");
            cmbVatTuHangHoa.Buttons[1].Visible = false;
            #endregion
        }
    }
}
